package com.midea.ai.air.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import org.json.JSONException;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * qiansong
 */
public class DeviceInfoUtil {
    private static final int BUFFER_SIZE = 1024;
    private static final String TAG = "DeviceInfo";

    /**
     * 获取versioncode
     *
     * @param context
     * @return
     * @throws JSONException
     */
    public static int getVersionCode(Context context) {
        int versionCode = -1;
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (Exception e) {
            L.e(TAG, e);
        }
        return versionCode;
    }

    /**
     * 获取versionName
     *
     * @param context
     * @return
     * @throws JSONException
     */
    public static String getVersionName(Context context) {
        String versionName = "";
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (Exception e) {
            L.e(TAG, e);
        }
        return versionName;
    }

    /**
     * 获取分辨率
     *
     * @param context
     * @throws JSONException
     */
    public static String getResolution(Context context) {
        String resolution = "";
        try {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            window.getDefaultDisplay().getMetrics(metrics);
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;
            int temp;
            if (width > height) {
                temp = width;
                width = height;
                height = temp;
            }
            resolution = String.valueOf(width) + "," + String.valueOf(height);
        } catch (Exception e2) {
            L.e(TAG, e2);

        }
        return resolution;
    }

    // private static void getLocalInfo(Context context, JSONObject result)
    // throws JSONException {
    // Configuration config = new Configuration();
    // Settings.System.getConfiguration(context.getContentResolver(), config);
    // TimeZone timeZone;
    // if ((config != null) && (config.locale != null)) {
    // result.put("Country", config.locale.getCountry());
    // result.put("Language", config.locale.toString());
    // Calendar calendar = Calendar.getInstance(config.locale);
    // if (calendar != null) {
    // timeZone = calendar.getTimeZone();
    // if (timeZone != null) {
    // result.put(LogConstants.TIME_ZONE, timeZone.getRawOffset() /
    // HOUR_TO_MILLS_UNIT);
    // } else {
    // result.put(LogConstants.TIME_ZONE, LogConstants.DEFAULT_TIME_ZONE);
    // }
    // } else {
    // result.put(LogConstants.TIME_ZONE, LogConstants.DEFAULT_TIME_ZONE);
    // }
    // } else {
    // Locale local = Locale.getDefault();
    // String country = local.getCountry();
    // if (!TextUtils.isEmpty((CharSequence) country)) {
    // result.put(LogConstants.COUNTRY, country);
    // } else {
    // result.put(LogConstants.COUNTRY, LogConstants.UNKNOWN);
    // }
    // String language = local.getLanguage();
    // if (!TextUtils.isEmpty(language)) {
    // result.put(LogConstants.LANGUAGE, language);
    // } else {
    // result.put(LogConstants.LANGUAGE, LogConstants.UNKNOWN);
    // }
    // Calendar calendar = Calendar.getInstance(local);
    // if (calendar != null) {
    // timeZone = calendar.getTimeZone();
    // if (timeZone != null) {
    // result.put(LogConstants.TIME_ZONE, ((TimeZone) timeZone).getRawOffset() /
    // HOUR_TO_MILLS_UNIT);
    // } else {
    // result.put(LogConstants.TIME_ZONE, LogConstants.DEFAULT_TIME_ZONE);
    // }
    // } else {
    // result.put(LogConstants.TIME_ZONE, LogConstants.DEFAULT_TIME_ZONE);
    // }
    // }
    // }

    /**
     * @return cpu info
     */
    public static String getCupInfo() {
        String str = null;
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader("/proc/cpuinfo");
            if (fileReader != null) {
                try {
                    bufferedReader = new BufferedReader(fileReader, BUFFER_SIZE);
                    str = bufferedReader.readLine();
                    bufferedReader.close();
                    fileReader.close();
                } catch (IOException ex) {
                    L.d(TAG, "读CPU信息失败");
                }
            }
        } catch (FileNotFoundException fileEx) {
            L.d(TAG, "读CPU信息失败");
        }

        return str.trim();
    }

    // /**
    // * @param context
    // * 上下文
    // * @return 可用网络
    // */
    // public static String[] getNetwork(Context context) {
    // String[] arrayOfString = { LogConstants.UNKNOWN, LogConstants.UNKNOWN };
    // PackageManager packageManager = context.getPackageManager();
    // if
    // (packageManager.checkPermission("android.permission.ACCESS_NETWORK_STATE",
    // context.getPackageName()) != 0) {
    // arrayOfString[0] = LogConstants.UNKNOWN;
    // return arrayOfString;
    // }
    // ConnectivityManager conManager = (ConnectivityManager)
    // context.getSystemService("connectivity");
    // if (conManager == null) {
    // arrayOfString[0] = LogConstants.UNKNOWN;
    // return arrayOfString;
    // }
    // NetworkInfo networkInfo = conManager.getNetworkInfo(1);
    // if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
    // arrayOfString[0] = "Wi-Fi";
    // return arrayOfString;
    // }
    // NetworkInfo otherNetworkInfo = conManager.getNetworkInfo(0);
    // if (otherNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
    // arrayOfString[0] = LogConstants.GSM;
    // arrayOfString[1] = otherNetworkInfo.getSubtypeName();
    // return arrayOfString;
    // }
    // return arrayOfString;
    // }

    /**
     * @param context 上下文
     * @return 设备ID
     */
    public static String getDeviceId(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = "";
        try {
            if (PackageUtil.checkPermission(context, "android.permission.READ_PHONE_STATE")) {
                deviceId = telephonyManager.getDeviceId();
            }
        } catch (Exception ex) {
            L.e(TAG, ex);
        }
        if (TextUtils.isEmpty(deviceId)) {
            L.d(TAG, "No IMEI.");
            deviceId = getMacAddress(context);
            if (deviceId == null) {
                L.e(TAG, "Failed to take mac as IMEI.");
            }
        }
        return deviceId;
    }

    /**
     * @param paramContext paramContext
     * @return String
     * @throws
     * @Method: getMacAddress
     */
    public static String getMacAddress(Context paramContext) {
        String result = null;
        try {
            WifiManager wifiManager = (WifiManager) paramContext.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            result = wifiInfo.getMacAddress();
        } catch (Exception ex) {
            L.d(TAG, "不能读mac地址");
        }
        return result;
    }


}
